package com.loma.common.utils.ext

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.view.View
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.loma.common.utils.util.isLightColor


/**
 * Created by xiaojianjun on 2019-11-21.
 */

fun Fragment.openInExplorer(link: String?) {
    startActivity(Intent().apply {
        action = Intent.ACTION_VIEW
        data = Uri.parse(link)
    })
}

/**
 * 设置沉浸式状态栏
 * @param colorResId 状态栏颜色id R.clore.white
 */

fun Fragment.initImmersiveStatusBar(color: Int) {
    val colorrbg = ContextCompat.getColor(requireContext(), color)
    if (requireContext().isLightColor(colorrbg)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val decorView: View = requireActivity().window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
        } else {
            requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }

        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true).statusBarColor(color).init()

    } else {
        ImmersionBar.with(this).statusBarDarkFont(false).fitsSystemWindows(true).statusBarColor(color).init()
    }


}

//fun Fragment.initImmersiveStatusBarWhitImage(drawable: Int) {
//
//    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//        val decorView: View = requireActivity().window.decorView
//        val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//        decorView.systemUiVisibility = option
//    } else {
//        requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//    }
//
//    ImmersionBar.with(this).statusBarDarkFont(false).fitsSystemWindows(true).
//
//
//}
